package com.clms.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clms.common.R;
import com.clms.system.mapper.RoleMapper;
import com.clms.system.pojo.entitry.SysRole;
import com.clms.system.pojo.vo.RoleVo;
import com.clms.system.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.*;

/**
 * @program: clms-third
 * @description:
 * @author: 大鱼
 * @create: 2021-07-19 18:07
 **/
@Service
public class RoleServiceimpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Override
    public R getRole(Integer userid) {
        List<SysRole> allRole = roleMapper.selectList(null);
        List<SysRole> haveRole = roleMapper.slelectbyid(userid);
        List<Integer> userhaveRole = new ArrayList<>();
        for (SysRole role: haveRole
             ) {
            userhaveRole.add((int) role.getRoleId());

        }
        return R.ok().put("roleAll",allRole).put("userRole",userhaveRole);
    }
    @Transactional
    @Override
    public R confirmRole(List<Integer> userRoleIds, Integer userId) {
        roleMapper.deleteByuserId(userId);
        roleMapper.insertrole(userRoleIds, userId);
        return R.ok();
    }
    @Transactional
    @Override
    public R initRoleTable(Integer currentPage, Integer pageSize, RoleVo roleVo) {
        Page<SysRole> page = new Page<>(currentPage,pageSize);
        roleMapper.selectList(null);
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty(roleVo.getRoleName())) {
            queryWrapper.eq("role_name",roleVo.getRoleName());
        }
        if (StringUtils.isNotEmpty(roleVo.getStartDate())) {
            queryWrapper.ge("creat_time", roleVo.getStartDate());
        }
        if (StringUtils.isNotEmpty(roleVo.getEndDate())) {
            queryWrapper.le("creat_time",roleVo.getEndDate());
        }
        roleMapper.selectPage(page, queryWrapper);
        return R.ok().put("record", page.getRecords()).put("total", page.getTotal());
    }
    @Override
    public R updateRoleStatus(Integer roleId, Integer status) {
        roleMapper.updateRoleStatus(roleId, status);
        return R.ok().message("修改成功");
    }

    @Override
    public R removerole(Integer roleId) {
        roleMapper.deleteById(roleId);
        return R.ok().message("删除成功");
    }

    @Override
    public R userAddOrUpdate(SysRole role) {
        if (role.getRoleId() != 0) {
            roleMapper.updateById(role);
        } else {
            roleMapper.insert(role);
        }

        return R.ok().message("更新成功");
    }

    @Override
    public R getUserInfo(Integer roleId) {
    SysRole role= roleMapper.selectById(roleId);
        return R.ok().put("roleinfo",role);
    }


}
